package com.xsky.validator
{
	import mx.validators.ValidationResult;
	import mx.validators.Validator;

	public class IPValidator extends Validator
	{
		private var ipInValid:String = "IP地址格式错误";
		private var ipInValidCode:String = '10001';
		protected var ip:RegExp = /^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/
		
		public function IPValidator()
		{
			super();
		}
		public function get ipInvalidError():String
		{
			return ipInValid;
		}
		public function set ipInvalidError(invalidStr:String):void
		{
			this.ipInValid = invalidStr;
		}
		public function doIpValidation(validator:IPValidator, value:Object, baseField:String):Array
		{
			var rs:Array = [];
			if(!ip.test(String(value)))
			{
				rs.push(new ValidationResult(true, baseField,validator.ipInValidCode, validator.ipInValid));
			}
			return rs;
		}
		override protected function doValidation(value:Object):Array
		{
			var rs:Array = super.doValidation(value);
			var val:String = value?String(value):'';
			if(rs.length>0||(val.length==0)&&!required)
			{
				return rs;
			}
			return doIpValidation(this,value,null);
		}
	}
}